<?php
 /**
 * Fonctions de gestion des disciplines
 * @package Garennes
 * @author Jean-Francois Campan <jean-francois.campan@ac-poitiers.fr>
 * @version 0.1
 */

 /**
 * Mise a jour des types de disciplines
 * @param int $millesime Annee courante
 */
 Function garennes_mise_a_jour_types_disciplines ($millesime)
  {
   Global $id_sql ;
   
   $types_disciplines = SQL_commande ("SELECT id_type_discipline,code FROM types_disciplines", $id_sql) ;
   $nbr_types = SQL_nbr_lignes ($types_disciplines) ;
   For ($i = 0 ; $i < $nbr_types ; $i++)
    {
     $type = SQL_ligne_suivante ($types_disciplines) ;
     If ($type [1] != "")
      {
       $tp = Explode ("|", $type [1]) ;
       If (SizeOf ($tp) > 1)
        {
	 For ($j = 1 ; $j <= SizeOf ($tp) ; $j++)
	  {
	   If ($tp [$j] != "")
	    {
	     $disciplines = SQL_commande ("SELECT id_discipline FROM disciplines WHERE code LIKE '".$tp [$j]."%' AND millesime='".$millesime."'", $id_sql) ;
	     $nbr_disc = SQL_nbr_lignes ($disciplines) ;
	     For ($k = 0 ; $k < $nbr_disc ; $k++)
	      {
	       $disc = SQL_ligne_suivante ($disciplines) ;
	       $ok = SQL_commande ("UPDATE disciplines SET id_type_discipline='".$type [0]."' WHERE id_discipline='".$disc [0]."' LIMIT 1", $id_sql) ;
	      }
	    }
	  }
	}
      }
    }
  }
 
 /**
 * Efface les disciplines non utilisees
 * @param int $millesime Annee courante
 */
 Function garennes_effacer_disciplines_inutiles ($millesime)
  {
   Global $id_sql ;
   $disciplines = SQL_commande ("SELECT id_discipline FROM disciplines WHERE millesime='".$millesime."' ORDER BY nom_long,nom_court,nom_bref,code", $id_sql) ;
   $nbr_disc = SQL_nbr_lignes ($disciplines) ;
   $ok = 0 ;
   $requete = "" ;
   For ($i = 0 ; $i < $nbr_disc ; $i++)
    {
     $disc = SQL_ligne_suivante ($disciplines) ;
     $verif_disc = SQL_commande ("SELECT id_service_prof FROM services_prof WHERE discipline='".$disc [0]."' LIMIT 1", $id_sql) ;
     If (SQL_nbr_lignes ($verif_disc) == 0)
      {
       If ($ok > 0) { $requete .= " OR " ; }
       $requete .= "id_discipline='".$disc [0]."'" ;
       $ok++ ;
      }
    }
   If ($ok > 0) { $ok = SQL_commande ("DELETE FROM disciplines WHERE ".$requete, $id_sql) ; }
  }
?>